//
//  QYSocketRequest.swift
//  QYNetworkSDK
//
//  Created by blazer on 2023/7/4.
//

import UIKit
/*
import PromiseKit

@objc public class QYSocketRequest: NSObject {
    private var urlSession: URLSession! = nil
    private var urlStreamTask: URLSessionStreamTask? = nil
    private var configure: URLSessionConfiguration = URLSessionConfiguration.default
    
    private let host: String
    private let port: Int
    
    private var response: ((Swift.Result<Data, QYHTTPSocketError>)->Void)? = nil
    private var callback: ((Data?, Error?)->Void)? = nil
    
    private var semphore: DispatchSemaphore = DispatchSemaphore(value: 0)
    
    @objc public init(host: String, port: Int) {
        self.host = host
        self.port = port
        configure.allowsCellularAccess = false
        configure.httpShouldUsePipelining = true
        urlSession = URLSession.init(configuration: configure)
        super.init()
    }
    
    @objc public func request(_ msg: Data, response: @escaping (Data?, Error?) -> Void) {
        write(msg).then { _ in
            self.read()
        }.done { data in
            response(data, nil)
        }.catch { error in
            response(nil, error)
        }
    }
    
    public func request(_ msg: Data) -> Promise<Data> {
        return Promise { seal in
            write(msg).then { _ in
                self.read()
            }.done { data in
                seal.fulfill(data)
            }.catch { error in
                seal.reject(error)
            }
        }
    }
    
    @objc public func close() {
        closeTask()
    }
}

private extension QYSocketRequest {
    func write(_ msg: Data) -> Promise<String> {
        return Promise { seal in
            if (urlStreamTask == nil) {
                let result = openTask()
                if (result == false) {
                    // 失败回调
                    seal.reject(QYHTTPSocketError.connectFailed)
                    return
                }
            }
            urlStreamTask!.write(msg, timeout: 2) { error in
                if (error != nil) {
                    seal.reject(QYHTTPSocketError.eof)
                } else {
                    seal.fulfill("")
                }
            }
        }
    }
    
    func read() -> Promise<Data> {
        return Promise { seal in
            guard let task = urlStreamTask else {
                seal.reject(QYHTTPSocketError.connectFailed)
                return
            }
            
            task.readData(ofMinLength: 1, maxLength: 1000, timeout: 2) { data, _, error in
                if error != nil {
                    seal.reject(error!)
                } else {
                    guard let d = data else {
                        seal.reject(QYHTTPSocketError.noData)
                        return
                    }
                    seal.fulfill(d)
                }
            }
        }
    }

    private func openTask() -> Bool {
        urlStreamTask = urlSession.streamTask(withHostName: host, port: port)
        guard let task = urlStreamTask else {
            return false
        }
        task.resume()
        return true
    }
    
    private func closeTask() {
        guard let task = urlStreamTask else {
            return
        }
        
        if task.state != .completed {
            if (task.state == .suspended) {
                task.resume()
            }
            task.closeWrite()
            task.closeRead()
        }
        urlStreamTask = nil
    }
}
*/
